package com.uyayo.notepad.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.common.exception.CategoryNotFoundException;
import com.uyayo.framework.common.exception.ModifiedRecordNotExistException;
import com.uyayo.framework.core.service.impl.BaseServiceImpl;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.notepad.entity.Note;
import com.uyayo.notepad.entity.NoteCategory;
import com.uyayo.notepad.mapper.NoteCategoryMapper;
import com.uyayo.notepad.mapper.NoteMapper;
import com.uyayo.notepad.model.NoteCondition;
import com.uyayo.notepad.service.NoteService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("noteService")
public class NoteServiceImpl extends BaseServiceImpl<Note> implements NoteService {

	private final NoteMapper noteMapper;

	private final NoteCategoryMapper noteCategoryMapper;

	public NoteServiceImpl(NoteMapper noteMapper, NoteCategoryMapper noteCategoryMapper) {
		this.noteMapper = noteMapper;
		this.noteCategoryMapper = noteCategoryMapper;
	}

	@Override
	public List<Note> selectNote(NoteCondition noteCondition) {
		Example example = new Example(Note.class);
		example.createCriteria();
		return noteMapper.selectByExample(example);
	}
	
	@Override
	public PageBean<Note> selectNoteByPaging(PageBean<Note> pageBean,NoteCondition noteCondition) {

		List<Note> userList;
		//分页处理
		List<OrderBy> orderByList = new ArrayList<>();
		orderByList.add(new OrderBy("createTime", "create_time"));
		orderByList.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderByList, pageBean.getOrderField(), pageBean.getOrderDirection());
		PageHelper.startPage(pageBean.getPageCurrent(), pageBean.getPageSize());
		
		Example example = new Example(Note.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(noteCondition.getTitle())){
			criteria.andLike("title","%"+noteCondition.getTitle()+"%");
		}
		if(Objects.nonNull(noteCondition.getMemberId())){
			criteria.andEqualTo("memberId",noteCondition.getMemberId());
		}
		if(Objects.nonNull(noteCondition.getNoteCategoryId())){
			criteria.andEqualTo("noteCategoryId",noteCondition.getNoteCategoryId());
		}
		if(noteCondition.getBeginCreateTime()!=null || noteCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", noteCondition.getBeginCreateTime(), noteCondition.getEndCreateTime());
		}
		userList = noteMapper.selectByExample(example);

		//取记录总条数
		PageInfo<Note> pageInfo = new PageInfo<>(userList);
		pageBean.setTotalCount(pageInfo.getTotal());
		pageBean.setList(userList);
		return pageBean;
	}
	

	@Override
	public int saveNote(Note note) throws CategoryNotFoundException {
		Long noteCategryId = note.getNoteCategoryId();
		NoteCategory noteCategory = noteCategoryMapper.selectByPrimaryKey(noteCategryId);
		if(noteCategory==null){
			throw new CategoryNotFoundException("参数异常，分类不存在，请刷新重试或联系管理员！");
		}
		Integer contentCount = noteCategory.getContentCount();
		noteCategory.setContentCount(contentCount+1);
		noteCategoryMapper.updateByPrimaryKeySelective(noteCategory);
		return noteMapper.insertSelective(note);
	}

	@Override
	public int updateNote(Note note) throws CategoryNotFoundException, ModifiedRecordNotExistException {
		Long noteCategryId = note.getNoteCategoryId();
		NoteCategory noteCategory = noteCategoryMapper.selectByPrimaryKey(noteCategryId);
		if(noteCategory==null){
			throw new CategoryNotFoundException("参数异常，分类不存在，请刷新重试或联系管理员！");
		}

		Long noteId = note.getId();
		Note noteOld = noteMapper.selectByPrimaryKey(noteId);
		if(noteOld==null){
			throw new ModifiedRecordNotExistException("参数异常，要修改的记录不存在，请刷新重试或联系管理员！");
		}
		Long noteCategryIdOld = noteOld.getNoteCategoryId();

		if(!Objects.equals(noteCategryId,noteCategryIdOld)){
			Integer contentCount = noteCategory.getContentCount();
			noteCategory.setContentCount(contentCount+1);
			noteCategoryMapper.updateByPrimaryKeySelective(noteCategory);

			NoteCategory noteCategoryOld = noteCategoryMapper.selectByPrimaryKey(noteCategryIdOld);
			if(noteCategoryOld!=null && noteCategoryOld.getContentCount()>0){
				Integer contentCountOld = noteCategoryOld.getContentCount();
				noteCategoryOld.setId(noteCategryIdOld);
				noteCategoryOld.setContentCount(contentCountOld-1);
				noteCategoryMapper.updateByPrimaryKeySelective(noteCategoryOld);
			}
		}
		return noteMapper.updateByPrimaryKeySelective(note);
	}
}
